import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public
class DBOsoba extends  Frame implements WindowListener, ActionListener
{
	Button buttonExit;
	Button buttonAdd;
	Button buttonSave;
	Button buttonRemove;
	Button buttonFirst;
	Button buttonLast;
	Button buttonNext;
	Button buttonPrevious;
	TextField tfImie;
	TextField tfNazwisko;
	TextField tfAdres;
	TextField tfTelefon;
	TextField tfEmail;
	Label lImie;
	Label lNazwisko;
	Label lAdres;
	Label lTelefon;
	Label lOsoba;
	
	protected Connection connection = null;
	protected Statement stm = null;
	protected ResultSet rs = null;
	protected LocalTableOsoba lto = null;

	public DBOsoba()
	{
		super();
		addWindowListener(this);
		setSize(450, 300);
		setTitle("DBOsoba");

		Label lImie = new Label();
		lImie.setBounds(20, 28, 30, 10);
		lImie.setText("Imie");
		add(lImie);

		tfImie = new TextField();
		tfImie.setBounds(20, 40, 150, 20);
		add(tfImie);
		
		Label lNazwisko = new Label();
		lNazwisko.setBounds(190, 28, 70, 10);
		lNazwisko.setText("Nazwisko");
		add(lNazwisko);
		tfNazwisko = new TextField();
		tfNazwisko.setBounds(190, 40, 150, 20);
		add(tfNazwisko);
		
		Label lAdres = new Label();
		lAdres.setBounds(20, 68, 70, 10);
		lAdres.setText("Adres");
		add(lAdres);

		tfAdres = new TextField();
		tfAdres.setBounds(20, 80, 320, 20);
		add(tfAdres);
		
		Label lTelefon = new Label();
		lTelefon.setBounds(20, 108, 50, 10);
		lTelefon.setText("Telefon");
		add(lTelefon);

		tfTelefon = new TextField();
		tfTelefon.setBounds(20, 120, 150, 20);
		add(tfTelefon);
		
		Label lEmail = new Label();
		lEmail.setBounds(190, 108, 70, 10);
		lEmail.setText("Email");
		add(lEmail);

		tfEmail = new TextField();
		tfEmail.setBounds(190, 120, 150, 20);
		add(tfEmail);

		buttonExit = new Button("Exit");
		buttonExit.setBounds(380, 260, 50, 20);
		buttonExit.addActionListener(this);
		add(buttonExit);
		
		buttonAdd = new Button("Add");
		buttonAdd.setBounds(380, 38, 50, 20);
		buttonAdd.addActionListener(this);
		add(buttonAdd);
		
		buttonSave = new Button("Save");
		buttonSave.setBounds(380, 68, 50, 20);
		buttonSave.addActionListener(this);
		add(buttonSave);
		
		buttonRemove = new Button("Remove");
		buttonRemove.setBounds(380, 98, 50, 20);
		buttonRemove.addActionListener(this);
		add(buttonRemove);

		buttonFirst = new Button("First");
		buttonFirst.setBounds(20, 160, 70, 20);
		buttonFirst.addActionListener(this);
		add(buttonFirst);
		
		buttonPrevious = new Button("Previous");
		buttonPrevious.setBounds(103, 160, 70, 20);
		buttonPrevious.addActionListener(this);
		add(buttonPrevious);
		buttonNext = new Button("Next");
		buttonNext.setBounds(184, 160, 70, 20);
		buttonNext.addActionListener(this);
		add(buttonNext);

		buttonLast = new Button("Last");
		buttonLast.setBounds(269, 160, 70, 20);
		buttonLast.addActionListener(this);
		add(buttonLast);

		setLayout(null);
		setVisible(true);

		connect("test");
		lto = new LocalTableOsoba();
		refresh();
	}
	public static void main(String args[])
	{
		new DBOsoba();
	}
	public void connect(String databaseName)
	{
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch(ClassNotFoundException e){
			showMessage("Driver error!", e.toString());
			System.exit(-1);
		}
		try{
		connection = DriverManager.getConnection("jdbc:odbc:" + databaseName);
			stm = connection.createStatement();
		}
		catch(SQLException e){
			showMessage("Connection error!", e.toString());
			System.exit(-1);
		}
	}
	public void fillData()
	{
		tfImie.setText(lto.IMIE);
		tfNazwisko.setText(lto.NAZWISKO);
		tfAdres.setText(lto.ADRES);
		tfTelefon.setText(lto.TELEFON);
		tfEmail.setText(lto.EMAIL);
	}
	public void refresh()
	{
		String query = "SELECT * FROM OSOBA;";
		try{
			rs = stm.executeQuery(query);
		}
		catch(SQLException e){
			showMessage("Refresh error!", e.toString());
		}
		lto.refresh(rs);
		fillData();
	}
	public void closeConnection()
	{
		try{
			connection.close();
		}
		catch(SQLException e){
		}
	}
	public void addClicked()
	{
		String query = "INSERT INTO OSOBA (IMIE, NAZWISKO, ADRES,";
		query += "TELEFON, EMAIL) VALUES('";
		query += tfImie.getText() + "','";
		query += tfNazwisko.getText() + "',";
		String temp = tfAdres.getText();
		if ("<brak danych>".equals(temp) || "".equals(temp)){
			query += "null, ";
		}
		else{
			query += "'" + tfAdres.getText() + "', ";
		}
		temp = tfTelefon.getText();
		if ("<brak danych>".equals(temp) || "".equals(temp)){
			query += "null, ";
		}
		else{
			query += "'" + tfTelefon.getText() + "', ";
		}
		temp = tfEmail.getText();
		if ("<brak danych>".equals(temp) || "".equals(temp)){
			query += "null)";
		}
		else{
			query += "'" + tfEmail.getText() + "')";
		}
		try{
			stm.executeUpdate(query);
		}
		catch(SQLException e){
			showMessage("Error: save", e.toString());
			return;
		}
		refresh();
	}
	public void saveClicked()
	{
		String query = "UPDATE OSOBA SET ";
		query += "IMIE = '" + tfImie.getText() + "',";
		query += "NAZWISKO = '" + tfNazwisko.getText() + "',";
		String temp = tfAdres.getText();
		if ("<brak danych>".equals(temp) || "".equals(temp)){
			query += "ADRES = null, ";
		}
		else{
			query += "ADRES = '" + tfAdres.getText() + "', ";
		}
		temp = tfTelefon.getText();
		if ("<brak danych>".equals(temp) || "".equals(temp)){
			query += "TELEFON = null, ";
		}
		else{
			query += "TELEFON = '" + tfTelefon.getText() + "', ";
		}
		temp = tfEmail.getText();
		if ("<brak danych>".equals(temp) || "".equals(temp)){
			query += "null)";
		}
		else{
			query += "EMAIL = '" + tfEmail.getText() + "' ";
		}
		query += "WHERE id = " + Integer.toString(lto.id);
		try{
			stm.executeUpdate(query);
		}
		catch(SQLException e){
			showMessage("Error: save", e.toString());
			return;
		}
		refresh();
	}
	public void removeClicked()
	{
		String query = "DELETE FROM OSOBA WHERE id=";
		query += Integer.toString(lto.id);
		try{
			stm.executeUpdate(query);
		}
		catch(SQLException e){
			showMessage("Error: remove", e.toString());
		}
		refresh();
	}
	public void firstClicked()
	{
		lto.first();
		fillData();
	}
	public void lastClicked()
	{
		lto.last();
		fillData();
	}
	public void nextClicked()
	{
		lto.next();
		fillData();
	}
	public void previousClicked()
	{
		lto.previous();
		fillData();
	}
	public void showMessage(String message, String error)
	{
		System.out.println(message + " ;" + error);
	}
	public void actionPerformed(ActionEvent e)
	{
		String command = e.getActionCommand();
		if (command.equals("Exit")){
			exitClicked();
		}
		else if (command.equals("Add")){
			addClicked();
		}
		else if (command.equals("Save")){
			saveClicked();
		}
		else if (command.equals("Remove")){
			removeClicked();
		}
		else if (command.equals("First")){
			firstClicked();
		}
		else if (command.equals("Last")){
			lastClicked();
		}
		else if (command.equals("Previous")){
			previousClicked();
		}
		else if (command.equals("Next")){
			nextClicked();
		}
	}
	public void exitClicked()
	{
		closeConnection();
		System.exit(0);
	}
	public void windowClosing(WindowEvent e)
	{
		exitClicked();
	}
	public void windowClosed(WindowEvent e)
	{
	}
	public void windowOpened(WindowEvent e)
	{
	}
	public void windowIconified(WindowEvent e)
	{
	}
	public void windowDeiconified(WindowEvent e)
	{
	}
	public void windowActivated(WindowEvent e)
	{
	}
	public void windowDeactivated(WindowEvent e)
	{
	}
}
